package com.frank.shardingJdbcMybatis.web;

import com.frank.shardingJdbcMybatis.entity.Address;
import com.frank.shardingJdbcMybatis.entity.User;
import com.frank.shardingJdbcMybatis.mapper.AddressMapper;
import com.frank.shardingJdbcMybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * @author 小石潭记
 * @date 2020/10/14 10:27
 * @Description: ${todo}
 */
@RestController
@RequestMapping("/sharding")
public class ShardingController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AddressMapper addressMapper;

    @GetMapping("/user/save")
    public String save() {
        for (int i = 0; i <10 ; i++) {
            User user=new User();
            user.setName("test"+i);
            user.setCityId(1%2==0?1:2);
            user.setSex(i%2==0?1:2);
            user.setPhone("11111111"+i);
            user.setEmail("xxxxx");
            user.setCreateTime(LocalDate.now());
            user.setPassword("eeeeeeeeeeee");
            userMapper.save(user);
        }
        return "success";
    }

    @GetMapping("/user/get/{id}")
    public User get(@PathVariable Long id) {
        User user =  userMapper.get(id);
        System.out.println(user.getId());
        return user;
    }

    @GetMapping("/address/save")
    public String saveAddress() {
        for (int i = 0; i <10 ; i++) {
            Address address=new Address();
            address.setCode("code_"+i);
            address.setName("name_"+i);
            address.setPid(i+"");
            address.setType(0);
            address.setLit(i%2==0?1:2);
            addressMapper.save(address);
        }
        return "success";
    }

    @GetMapping("/address/get/{id}")
    public Address getAddress(@PathVariable Long id) {
        return addressMapper.get(id);
    }

}
